package com.life.ai.server.common.domain;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;

/**
 * 分页查询实体类
 * PageQuery
 *
 * @author kylewka
 * @since 2023/9/7
 */
@Data
public class PageQuery implements Serializable {

    /**
     * 分页大小
     */
    private Integer size;

    /**
     * 当前页数
     */
    private Integer current;

    /**
     * 是否简单模式
     */
    private Boolean simple = false;

    /**
     * 当前记录起始索引 默认值
     */
    public static final int DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数 默认值 默认查全部
     */
    public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;

    public PageQuery(Integer current, Integer size) {
        this.size = size;
        this.current = current;
    }

    public <T> Page<T> build() {
        Integer pageNum = ObjectUtil.defaultIfNull(getCurrent(), DEFAULT_PAGE_NUM);
        Integer pageSize = ObjectUtil.defaultIfNull(getSize(), DEFAULT_PAGE_SIZE);
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return new Page<>(pageNum, pageSize);
    }

    public static PageQuery of(Integer current, Integer size) {
        return new PageQuery(current, size);
    }

    public static PageQuery all() {
        return new PageQuery(1, -1);
    }



}
